package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {
    @Reference
    private CommunityService communityService;
    @Reference
    private DictService dictService;
    private static final String PAGE_INDEX = "community/index";
    private static final String PAGE_CREATE = "community/create";
    private static final String PAGE_EDIT = "community/edit";
    private static final String PAGE_DELETE= "redirect:/community";

    @RequestMapping
    public String index(@RequestParam Map<String, Object> filters, Model model) {
        //处理pageNum和pageSize为空的情况
        if (filters.get("pageNum") == null || "".equals(filters.get("pageNum"))) {
            filters.put("pageNum", 1);
        }
        if (filters.get("pageSize") == null || "".equals(filters.get("pageSize"))) {
            filters.put("pageSize", 10);
        }
        //查询"beijing"的所有区域
        List<Dict> areaList = dictService.findDictListByParentDictCode("beijing");
        PageInfo<Community> pageInfo = communityService.findPage(filters);

        model.addAttribute("page", pageInfo);
        //查询beijing的所有区域列表，保存到请求域中
        saveAreaListToModel(model);

        //搜索条件中可能不包含areaId和plateId，但是页面回显的时候需要，所以我们判断filters中是否包含了areaId和plateId
        //如果不包含，那么就设置为0
        if (filters.get("areaId") == null || "".equals(filters.get("areaId"))) {
            filters.put("areaId", 0);
        }
        if (filters.get("plateId") == null || "".equals(filters.get("plateId"))) {
            filters.put("plateId", 0);
        }
        //5. 搜索回显到页面，将filters搜索条件存储到请求域
        model.addAttribute("filters", filters);
        return PAGE_INDEX;
    }

    /**
     * 查询北京的所有区域列表，保存到请求域中
     *
     * @param model
     */
    private void saveAreaListToModel(Model model) {
        //4. 查询"beijing"的所有的区域列表
        List<Dict> areaList = dictService.findDictListByParentDictCode("beijing");
        //将查询到的区域列表保存到request域中
        model.addAttribute("areaList", areaList);
    }

    @RequestMapping("/create")
    public String create(Model model) {
        saveAreaListToModel(model);
        return PAGE_CREATE;
    }

    @PostMapping("/save")
    public String save(Community community, Model model) {
        communityService.insert(community);
        return successPage(model, "添加小区成功！");
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        //查询小区信息
        Community community = communityService.getById(id);
        model.addAttribute("community", community);
        saveAreaListToModel(model);
        return PAGE_EDIT;
    }

    @PostMapping("/update")
    public String update(Community community, Model model) {
        communityService.update(community);
        return successPage(model, "修改小区成功");
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        communityService.delete(id);
        //重定向访问首页
        return PAGE_DELETE;
    }

}
